package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Log;
import bean.Mail;
import bean.User;
import dao.LogManagerDao;
import dao.LogManagerDaoImpl;
import dao.MailManagerDao;
import dao.MailManagerDaoImpl;

/**
 * Servlet implementation class massMail
 */
@WebServlet("/massMail")
public class massMail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public massMail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String content=request.getParameter("Content");
		HttpSession session = request.getSession();
		User user=(User) session.getAttribute("user");
		LogManagerDao logdao =new LogManagerDaoImpl();
		Mail mail=new Mail();
		mail.setContent(content);
		MailManagerDao mmd=new MailManagerDaoImpl();
		mail=mmd.addMail(mail);
		if(mail!=null){
			int mailid=mail.getMail_id();
			if(mmd.massMailofUser(mailid)==true){
				Log log=new Log();
				log.setExcutor(user.getUser_id());
				log.setExcution("发送邮件");
				log.setState(true);
				logdao.addLog(log);
			}
			else{
				Log log=new Log();
				log.setExcutor(user.getUser_id());
				log.setExcution("发送邮件");
				log.setState(false);
				logdao.addLog(log);
			}
		}
		else{
			Log log=new Log();
			log.setExcutor(user.getUser_id());
			log.setExcution("发送邮件");
			log.setState(false);
			logdao.addLog(log);
		}
		request.getRequestDispatcher("sendMails.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
